<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\bundle;

use yii\bootstrap4\BootstrapAsset;
use yii\bootstrap4\BootstrapPluginAsset;
use yii\web\AssetBundle;
use yii\web\JqueryAsset;
use yii\web\YiiAsset;

/**
 * 核心资源包
 * @author cleverstone
 * @since ym2.0
 */
class CoreAsset extends AssetBundle
{

    public $depends = [
        'jQuery' => JqueryAsset::class,
        'promise' => Promise4::class,
        'tinyMCE' => TinyMCE7::class,
        'angular' => Angular182::class,
        'fontawesome' => FontAwesome47::class,
        'lodash' => Lodash4::class,
        'sweetAlert' => SweetAlert211::class,
        'layui' => Layui28::class,
        'bootstrapCss' => BootstrapAsset::class,
        'bootstrapJs' => BootstrapPluginAsset::class,
        'bootstrapIcon' => BootstrapIcon::class,
        'highlight' => Highlight::class,
        'select2' => Select24::class,
        'angularSelect2' => AngularSelectUi::class,
        'select2Bootstrap4Theme' => Select2Bootstrap4Theme151::class,
        'angularUpload' => AngularUpload12::class,
        'spinner' => Spinner2::class,
        'toastr' => Toastr2::class,
        'yii' => YiiAsset::class,
        'uEditor' => Ueditor1433::class,
        'fullscreen' => Fullscreen::class,
        'formTag' => FormTag::class,
        'sortable' => Sortable::class,
        'encrypt' => Encrypt::class,
        'xmSelect' => XmSelect::class,
        'ym2Upload' => Ym2Upload::class,
        'animateCss' => Animate::class,
    ];

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();
        $this->excludeAsset();
    }

    /**
     * 根据[View::params]中'__dynamic_assets'中的注册，
     * 来设置是否引用指定包
     * 请动态设置`$dynamicIncludeAssets`变量
     * @return void
     */
    protected function excludeAsset()
    {
        $viewParams = \Yii::$app->view->params;
        $dynamicAssets = [];
        if (!empty($viewParams['__dynamic_assets'])) {
            $dynamicAssets = $viewParams['__dynamic_assets'];
        }

        $dynamicIncludeAssets = [
            'uEditor',
            'tinyMCE',
            'highlight',
        ];
        foreach ($dynamicIncludeAssets as $asset) {
            if (!in_array($asset, $dynamicAssets)) {
                unset($this->depends[$asset]);
            }
        }
    }
}